import { getRandomInt } from './number';
import { Song } from '@services/data-types/common.type';

export function inArray(arr: string[], target: string) {
  return arr.includes(target);
}

export function shuffle<T>(arr: T[]): T[] {
  const result = arr.slice();
  for (let i = 0; i < result.length; i++) {
    const j = getRandomInt([0, 1]);
    [result[i], result[j]] = [result[j], result[i]];
  }
  return result;
}

export function findIndex(list: Song[], currentSong: Song) {
  if (list.length && currentSong) {
    const realList = list.filter(item => item !== undefined);
    return realList.findIndex(item => item.id === currentSong.id);
  }
}
